package com.atguigu.spzx.manager.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.atguigu.spzx.manager.mapper.*;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.dto.sys.ProductSaveDto;
import com.atguigu.spzx.model.entity.brand.Brand;
import com.atguigu.spzx.model.entity.category.Category;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.product.ProductVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * title:
 * author: zgl
 * date: 2024/1/6
 * description：
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageInfo<ProductVo> getList(Integer page, Integer limit, ProductDto productDto) {
        //设置分页参数
        PageHelper.startPage(page, limit);
        //查询数据库
        List<ProductVo> list = productMapper.getList(productDto);
        return new PageInfo<>(list);
    }

    @Override
    public void save(ProductSaveDto productSaveDto) {
        //保存product并返回id
        Product product = new Product();
        BeanUtils.copyProperties(productSaveDto, product);
        product.setStatus(0);
        product.setAuditStatus(0);
        productMapper.save(product);

        //保存product_sku
        List<ProductSku> skuList = productSaveDto.getProductSkuList();
        Long productId = product.getId();
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        skuList.forEach(productSku -> {
            productSku.setProductId(productId);
            productSku.setSkuCode(snowflake.nextIdStr());
            productSku.setSkuName(productSaveDto.getName() + "" + productSku.getSkuSpec());
            productSku.setSaleNum(0);
            productSku.setStatus(0);
        });
        productSkuMapper.moreSave(skuList);

        //保存product_details
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(productId);
        productDetails.setImageUrls(productSaveDto.getDetailsImageUrls());
        productDetailsMapper.save(productDetails);
    }

    @Override
    public ProductSaveDto getById(Long id) {
        Product product = productMapper.getById(id);

        ProductDetails productDetails = productDetailsMapper.getById(id);

        List<ProductSku> productSkuList = productSkuMapper.getById(id);

        ProductSaveDto productSaveDto = new ProductSaveDto();
        BeanUtils.copyProperties(product, productSaveDto);
        productSaveDto.setDetailsImageUrls(productDetails.getImageUrls());
        productSaveDto.setProductSkuList(productSkuList);
        //回显三级菜单名称，品牌名称
        Brand brand = brandMapper.getBrandById(product.getBrandId());
        Category categoryOne = categoryMapper.getById(product.getCategory1Id());
        Category categoryTwo = categoryMapper.getById(product.getCategory2Id());
        Category categoryThree = categoryMapper.getById(product.getCategory3Id());
        productSaveDto.setBrandName(brand.getName());
        productSaveDto.setCategory1Name(categoryOne.getName());
        productSaveDto.setCategory2Name(categoryTwo.getName());
        productSaveDto.setCategory3Name(categoryThree.getName());
        return productSaveDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "spzx:cache",key = "'cates'")
    public void updateById(ProductSaveDto productSaveDto) {
        //修改product
        Product product = new Product();
        BeanUtils.copyProperties(productSaveDto, product);
        productMapper.updateById(product);

        //修改product_sku
        List<ProductSku> productSkuList = productSaveDto.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSku.setSkuName(product.getName() + " " + productSku.getSkuSpec());
            productSkuMapper.updateById(productSku);
        });

        //修改product_details
        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(product.getId());
        productDetails.setImageUrls(productSaveDto.getDetailsImageUrls());
        productDetailsMapper.updateById(productDetails);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        product.setAuditStatus(auditStatus);
        product.setAuditMessage("商品参数错误");
        productMapper.updateById(product);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(status);
        productMapper.updateById(product);
        productSkuMapper.updateByPid(id,status);
    }
}
